// pages/drinking/drinking.js
import http from '../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    familyName: "",
    isHouseholder: 0,
    machines: [],
    unBindShow: false,
    unBindIdx: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.shareTitle,
      path: '/pages/index/index'
    }
  },
  getInfo(){
    http.request("/machineManage/machines")
    .then(res=>{
      console.log(res.data);
      this.setData({
        address: res.data.address,
        familyName: res.data.familyName,
        isHouseholder:res.data.isHouseholder,
        machines: res.data.machines
      })
    })
    .catch(res=>{

    })
  },
  voiceSwitchChange(e){
    let is_voice_notice = e.detail.value
    let idx = e.currentTarget.dataset.idx
    this.setData({
      [`machines[${idx}].is_voice_notice`]: is_voice_notice ? 1 : 0
    })
  },
  naviToEditAddr(){
    if(this.data.isHouseholder==0&&this.data.familyName!=null&&this.data.familyName!=""){
      wx.showToast({
        title: '只有主账号才能编辑家庭信息！',
        icon: 'none'
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/editAddr/editAddr?familyName=' + this.data.familyName + "&address=" + this.data.address,
    })
  },
  // 无数据的情况下的绑定
  naviToBindDrinking(){
    wx.navigateTo({
      url: '/pages/bindDrinking/bindDrinking?hasNextStep=0',
    })
  },
  // 增加饮水机
  naviToAddDrinking(){
    if(this.data.isHouseholder==0){
      if(this.data.familyName!=null&&this.data.familyName!=""){
        wx.showToast({
          title: '只有主账号才能进行捆绑和解除捆绑！',
          icon: 'none'
        });
      }else{
        wx.showToast({
          title: '没创建家庭，请先创建家庭！',
          icon: 'none'
        });
      }
      return;
    }
    wx.navigateTo({
      url: '/pages/addDrinking/addDrinking',
    })
  },
  naviToRepair(e){
    let idx = e.currentTarget.dataset.idx
    wx.navigateTo({
      url: '/pages/repair/repair?deviceId=' + this.data.machines[idx].id,
    })
  },
  openUnBindShow(e){
    if(this.data.isHouseholder==0){
      wx.showToast({
        title: '只有主账号才能进行捆绑和解除捆绑！',
        icon: 'none'
      });
      return;
    }
    let unBindIdx = e.currentTarget.dataset.idx
    this.setData({
      unBindShow: true,
      unBindIdx: unBindIdx
    })
  },
  onClose(){
    this.setData({
      unBindShow: false
    })
  },
  unBind(){
    let params = {
      deviceId: this.data.machines[this.data.unBindIdx].device_code  // deviceId 要用device_code 后端的奇葩设定
    }
    http.request("/machine/unbind",params)
    .then(res=>{
      let machines = this.data.machines
      machines.splice(this.data.unBindIdx,1)
      this.setData({
        machines,
        unBindShow: false
      })
    })
  }
})